# import sqlite3
import pymysql
import scapy.all as scapy
import os
# import scapy_http.http

def conn_sqlite(attack_type,dst,dport,src,sport,proto,Attack_characteristics):
    # sqlite_conn = sqlite3.connect("user_information.db")
    # cur = sqlite_conn.cursor()
    # cur.execute('''CREATE TABLE IF NOT EXISTS warning_pcap_information(
    #                                                attack_type TEXT,
    #                                                dst TEXT,
    #                                                dport TEXT,
    #                                                src TEXT,
    #                                                sport TEXT,
    #                                                proto TEXT,
    #                                                Attack_characteristics TEXT);''')
    # sqlite_conn.commit()
    # cur.execute(f"INSERT INTO warning_pcap_information(attack_type,dst,dport,src,sport,proto,Attack_characteristics) values('{attack_type}','{dst}','{dport}','{src}','{sport}','{proto}','{Attack_characteristics}')")
    # sqlite_conn.commit()
    # sqlite_conn.close()

    db = pymysql.connect(
        host='localhost',
        user='root',
        password='123456',
        database='maoxuechunqimozuoye'
    )
    py_cursor = db.cursor()
    py_cursor.execute('''CREATE TABLE IF NOT EXISTS warning_pcap_information(
                                                       attack_type VARCHAR(255),
                                                       dst VARCHAR(255),
                                                       dport VARCHAR(255),
                                                       src VARCHAR(255),
                                                       sport VARCHAR(255),
                                                       proto VARCHAR(255),
                                                       Attack_characteristics VARCHAR(255))DEFAULT CHARSET=utf8;''')
    db.commit()
    py_cursor.execute(
        f"INSERT INTO warning_pcap_information(attack_type,dst,dport,src,sport,proto,Attack_characteristics) values('{attack_type}','{dst}','{dport}','{src}','{sport}','{proto}','{Attack_characteristics}')")
    db.commit()
    db.close()

def all_log():
    path = "Pcap_log"
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        packets = scapy.rdpcap(path + '/' + file)

        for p in packets:

            '''
            写代码解析日志文件，并存入数据库中
            '''

            db = pymysql.connect(
                host='localhost',
                user='root',
                password='123456',
                database='maoxuechunqimozuoye'
            )
            py_cursor = db.cursor()
            py_cursor.execute('''CREATE TABLE IF NOT EXISTS log_pcap_information(
                                                                       dst VARCHAR(256),
                                                                       dport VARCHAR(256),
                                                                       src VARCHAR(256),
                                                                       sport VARCHAR(256),
                                                                       proto VARCHAR(256))DEFAULT CHARSET=utf8;''')
            db.commit()
            py_cursor.execute(
                f"INSERT INTO log_pcap_information(dst,dport,src,sport,proto) values('{dst}','{dport}','{src}','{sport}','{proto}')")
            db.commit()
            db.close()

def XSS():            #XSS
    path = "Pcap_log"
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        packets = scapy.rdpcap(path + "/" + file)

        for p in packets:
            '''
            写代码解析日志文件，并存入数据库中
             '''


def Mining():            #挖矿
    path = "Pcap_log"
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        packets = scapy.rdpcap(path + '/' + file)

        for p in packets:
            '''
            写代码解析日志文件，并存入数据库中
            '''

def extortion():  #勒索
    path = "Pcap_log"
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        packets = scapy.rdpcap(path + '/' + file)

        for p in packets:
            '''
            写代码解析日志文件，并存入数据库中
            '''

def SQL(): #SQL注入
    path = "Pcap_log"
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        packets = scapy.rdpcap(path + '/' + file)

        '''
        写代码解析日志文件，并存入数据库中
        '''

def bingxie(): #冰蝎
    path = "Pcap_log"
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        packets = scapy.rdpcap(path + '/' + file)

        for p in packets:
            '''
            写代码解析日志文件，并存入数据库中
            '''


def upload(): #后门上传
    path = "Pcap_log"
    files = os.listdir(path)
    for file in files:  # 遍历文件夹
        packets = scapy.rdpcap(path + '/' + file)

        '''
        写代码解析日志文件，并存入数据库中
        '''


def main():
    XSS()
    Mining()
    extortion()
    SQL()
    bingxie()
    upload()
    all_log()